package cn.haut.online_read.crawler;

import cn.haut.online_read.utils.toMapList;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.*;

/**
 * @author xing
 * @date 2021-04-21 13:19
 */
@Slf4j
public class BookCrawler implements PageProcessor {

    private Site site = Site.me().setSleepTime(1);

    @Override
    public void process(Page page) {
        // 第几章
        List<String> indexList = page.getHtml().xpath("//ul[@id='J_chapterList']/li/a/@href")
                .replace(".html", "")
                .replace("/book/[0-9a-z]+/","")
                .all();
        // 章节名
        List<String> nameList = page.getHtml().xpath("//ul[@id='J_chapterList']/li/a/text()").all();

        // 最新章节 第几章
        List<String> newIndexList = page.getHtml().xpath("//ul[@class='chapter-list clearfix']/li/a/@href")
                .replace(".html", "")
                .replace("/book/[0-9a-z]+/","")
                .all();
        // 最新章节名
        List<String> newNameList = page.getHtml().xpath("//ul[@class='chapter-list clearfix']/li/a/text()").all();


        // name:  章节名
        // index: 章节索引，为章节对应链接里的 索引，，表示第几章
        List<String> namesList2 = Arrays.asList("name", "index");
        List<Map<String, Object>> list = toMapList.get(namesList2, nameList, indexList);

        List<Map<String, Object>> newList = toMapList.get(namesList2, newNameList, newIndexList);

        Map<String, Object> bookMap = new HashMap<>();
        // 书名
        String name = page.getHtml().xpath("//div[@class='book-info']/div/h1/text()").get();
        // 标签
        List<String> tagsNameList = page.getHtml().xpath("//p[@class='tags']/i/text()").all();
        // 标签背景颜色
        List<String> tagstyleList = page.getHtml().xpath("//p[@class='tags']/i/@style").all();
        // 放在一起
        String[] tagsNames = new String[]{"name", "style"};
        List<Map<String, Object>> tagsList = toMapList.get(Arrays.asList(tagsNames), tagsNameList, tagstyleList);

        // 书封面图片里阿尼额
        String imgUrl = page.getHtml().xpath("//div[@class='book-info']/img/@src").get();
        // 作者
        String author = page.getHtml().xpath("//div[@class='book-info']/div/p[2]/a/text()").get();
        // 分类 以及 总字数：
        Selectable xpath = page.getHtml().xpath("//div[@class='book-info']/div/p[2]/text()");
        String totalWords = xpath.replace("^[^0-9]+", "").get();
        String minor = xpath.replace(totalWords, "").get();

        // 最后更新日期
        String finalDate = page.getHtml().xpath("//div[@class='book-info']/div/p[3]/text()").get();
        // 书的数据
        List<String> dataKeyList = page.getHtml().xpath("//div[@class='book-data']//i[@class='key']/text()").all();
        List<String> dataValueList = page.getHtml().xpath("//div[@class='book-data']//i[@class='value']/text()").all();
        // 放在一起
        String[] dataNames = new String[]{"key", "value"};
        List<Map<String, Object>> dataList = toMapList.get(Arrays.asList(dataNames), dataKeyList, dataValueList);

        String desc = page.getHtml().xpath("//div[@class='book-section']/p/text()").get();

        String bookId = page.getHtml().xpath("//input[@id='J_bookId']/@value").get();

        /**
         *  name
         *  tags:
         *      name
         *      style
         *  imgUrl
         *  author
         *  classify
         *  finalDate
         *  date:
         *      key
         *      value
         *  desc
         */
        bookMap.put("id", bookId);
        bookMap.put("name", name);
        bookMap.put("tags", tagsList);
        bookMap.put("imgUrl", imgUrl);
        bookMap.put("author", author);
        bookMap.put("minor", minor);
        bookMap.put("totalWords", totalWords);
        bookMap.put("finalDate", finalDate);
        bookMap.put("data", dataList);
        bookMap.put("desc", desc);

        // list 前端用 ref 对象
        page.putField("chapters", list);
        page.putField("newChapters", newList);
        // map 前端用 reactive 对象
        page.putField("book", bookMap);
    }

    @Override
    public Site getSite() {
        return site;
    }
}
